package com.ebelter.bodyfatscale.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.ebelter.bodyfatscale.R;
import com.ebelter.bodyfatscale.moudules.beans.ComputerMesureData;
import com.ebelter.bodyfatscale.ui.view.LineView;
import com.ebelter.bodyfatscale.util.NumUtils;
import com.ebelter.bodyfatscale.util.ScaleBaseUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static com.ebelter.bodyfatscale.ui.pager.main.TrendsPager.TIME_DAY;
import static com.ebelter.bodyfatscale.ui.pager.main.TrendsPager.TIME_MONTH;
import static com.ebelter.bodyfatscale.ui.pager.main.TrendsPager.TIME_WEEK;
import static com.ebelter.bodyfatscale.ui.pager.main.TrendsPager.TYPE_BODYFAT;
import static com.ebelter.bodyfatscale.ui.pager.main.TrendsPager.TYPE_MUSCLE;
import static com.ebelter.bodyfatscale.ui.pager.main.TrendsPager.TYPE_WEIGHT;

/**
 * Created by pan on 2018/4/18
 */

public class TrendHLvAdapter extends RecyclerView.Adapter<TrendHLvAdapter.ViewHolder> {



    private Context mContext;

    private List<ComputerMesureData> dayDatas;
    private List<ComputerMesureData> weekDatas;
    private List<ComputerMesureData>  monthDatas;


    //1avgWeight 2avgAR 3avgMQ
    private int currentDisplayType=1;
    //4DAY 5WEEK 6MONTH
    private int currentDisplayTimeType=4;



    public void setCurrentDisplayTimeType(int currentDisplayTimeType) {
        this.currentDisplayTimeType = currentDisplayTimeType;
    }

    //设置当前显示的类型
    public void setCurrentDisplayType(int currentDisplayType) {
        this.currentDisplayType = currentDisplayType;
        calculator();
        notifyDataSetChanged();
    }





    public TrendHLvAdapter(Context context){
        this.mContext=context;
        initList();
        calculator();
    }



    private void initList(){
        if (dayDatas==null){
            dayDatas=new ArrayList<>();
        }
        if (weekDatas==null){
            weekDatas=new ArrayList<>();
        }
        if (monthDatas==null){
            monthDatas=new ArrayList<>();
        }
    }

    @NonNull
    @Override
    public TrendHLvAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(mContext,R.layout.trendhlv_item_ly,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TrendHLvAdapter.ViewHolder holder, int position) {

        float linePer = 0;
        float itemValue = 0;
        String time = "";

        try {

            List<ComputerMesureData> dataTemp = getDisplayDatas();
            if (dataTemp==null){
                linePer=0;
                itemValue=0;
                time="";
            }else {
                if (currentDisplayType==1){
                    itemValue=dataTemp.get(position).getAvgWeight();
                    linePer=maxWeight==0?0:(itemValue/maxWeight);
                }else if (currentDisplayType==2){
                    itemValue=dataTemp.get(position).getAvgAR();
                    linePer=maxAR==0?0:(itemValue/maxAR);
                }else if (currentDisplayType==3){
                    itemValue=dataTemp.get(position).getAvgMQ();
                    linePer=maxMQ==0?0:(itemValue/maxMQ);
                }
                if (currentDisplayTimeType==TIME_DAY){
                    time = ScaleBaseUtils.timeFatmat(dataTemp.get(position).getDays(),1);//1970-01-01 08:00:00
                }else if (currentDisplayTimeType==TIME_WEEK){
                    time = ScaleBaseUtils.intNumFatmat7(dataTemp.get(position).getDays());//2018-16 2018年16周的意思
                }else if(currentDisplayTimeType==TIME_MONTH){
                    time = ScaleBaseUtils.monthFatmat(dataTemp.get(position).getDays());//2018-06 2018年6月的意思
                }
            }


        }catch (Exception e){
            e.printStackTrace();
        }



        holder.lineView.setTopTitle(NumUtils.numBaoLiuWei(ScaleBaseUtils.adapterDataFloat(itemValue),1)+"");
        holder.lineView.setValues(linePer,time,1.0f);
    }

    @Override
    public int getItemCount() {
        if (currentDisplayTimeType==TIME_DAY){
            return dayDatas==null?0:dayDatas.size();
        }else if (currentDisplayTimeType==TIME_WEEK){
            return weekDatas==null?0:weekDatas.size();
        }else if(currentDisplayTimeType==TIME_MONTH){
            return monthDatas==null?0:monthDatas.size();
        }
        return 0;
    }

    private List<ComputerMesureData> getDisplayDatas(){
        if (currentDisplayTimeType==TIME_DAY){
           return dayDatas;
        }else if (currentDisplayTimeType==TIME_WEEK){
            return weekDatas;
        }else if(currentDisplayTimeType==TIME_MONTH){
            return monthDatas;
        }
        return null;
    }


    //whichTime 1DAY 2WEEK 3MONTH
    public void addDatas(List<ComputerMesureData> data, int whichTime) {
        if (whichTime==4){
            dayDatas=data;
        }else if (whichTime==5){
            weekDatas=data;
        }else if(whichTime==6){
            monthDatas=data;
        }
        calculator();
        notifyDataSetChanged();
    }


    //TYPE_WEIGHT 1
    //TYPE_BODYFAT 2
    //TYPE_MUSCLE 3
    //TIME_DAY 4
    //TIME_WEEK 5
    //TIME_MONTH 6
    public double getAvg(int TIME_TYPE,int GN_TYPE){
        List<ComputerMesureData> data=getDataByTimeType(TIME_TYPE);
        if (data==null||data.size()==0){
            return 0;
        }
        if (TYPE_WEIGHT==GN_TYPE){
            return data.get(data.size()-1).getAvgWeight();
        }else if (TYPE_BODYFAT==GN_TYPE){
            return data.get(data.size()-1).getAvgAR();
        }else if (TYPE_MUSCLE==GN_TYPE){
            return data.get(data.size()-1).getAvgMQ();
        }
        return 0;
    }

    //TYPE_WEIGHT 1
    //TYPE_BODYFAT 2
    //TYPE_MUSCLE 3
    //TIME_DAY 4
    //TIME_WEEK 5
    //TIME_MONTH 6
    public float getChange(int TIME_TYPE,int GN_TYPE){
        List<ComputerMesureData> data=getDataByTimeType(TIME_TYPE);
        if (data==null||data.size()==0||data.size()==1){
            return 0;
        }else if (data.size()>1){
            if (TYPE_WEIGHT==GN_TYPE){
                float f1= NumUtils.numBaoLiuWei(ScaleBaseUtils.adapterDataFloat(data.get(data.size()-1).getAvgWeight()),1);
                float f2=NumUtils.numBaoLiuWei(ScaleBaseUtils.adapterDataFloat(data.get(data.size()-2).getAvgWeight()),1);
                return Math.abs(f1-f2);
            }else if (TYPE_BODYFAT==GN_TYPE){
                float f3= NumUtils.numBaoLiuWei(data.get(data.size()-1).getAvgAR(),1);
                float f4=NumUtils.numBaoLiuWei(data.get(data.size()-2).getAvgAR(),1);
                return Math.abs(f3-f4);
            }else if (TYPE_MUSCLE==GN_TYPE){
                float f5 = NumUtils.numBaoLiuWei(ScaleBaseUtils.adapterDataFloat(data.get(data.size()-1).getAvgMQ()),1);
                float f6 = NumUtils.numBaoLiuWei(ScaleBaseUtils.adapterDataFloat(data.get(data.size()-2).getAvgMQ()),1);
                return Math.abs(f5-f6);
            }
        }

        return 0;
    }

    private List<ComputerMesureData> getDataByTimeType(int TIME_TYPE){
        List<ComputerMesureData> data=null;
        if (TIME_TYPE==TIME_DAY){
            data=dayDatas;
        }else if (TIME_TYPE==TIME_WEEK){
            data=weekDatas;
        }else if (TIME_TYPE==TIME_MONTH){
            data=monthDatas;
        }
        return data;
    }

    public void clearData() {
        if (dayDatas!=null){
            dayDatas.clear();
        }
        if (weekDatas!=null){
            weekDatas.clear();
        }
        if (monthDatas!=null){
            monthDatas.clear();
        }
    }


    //ViewHolder，需要继承RecyclerView.ViewHolder
    static class ViewHolder extends RecyclerView.ViewHolder{
        private LineView lineView;
        ViewHolder(View itemView) {
            super(itemView);
            lineView = itemView.findViewById(R.id.trendhlv_item_lineview);
        }
    }

    public float maxWeight;
    public float maxAR;
    public float maxMQ;
    public void calculator(){
        List<ComputerMesureData> datas = getDisplayDatas();;


        if (datas==null||datas.size()==0){
            maxWeight=0;
            maxAR=0;
            maxMQ=0;

        }else if (datas.size()==1){
            maxWeight=  datas.get(0).getAvgWeight();
            maxAR= datas.get(0).getAvgAR();
            maxMQ=  datas.get(0).getAvgMQ();
        }else {
            ComputerMesureData maxWeight1 = Collections.max(datas, new Comparator<ComputerMesureData>() {
                @Override
                public int compare(ComputerMesureData o1, ComputerMesureData o2) {
                    if (o1.getAvgWeight()<o2.getAvgWeight()){
                        return -1;
                    }else if (o1.getAvgWeight()>o2.getAvgWeight()){
                        return 1;
                    }else {
                        return 0;
                    }

                }
            });
            ComputerMesureData maxAR1= Collections.max(datas, new Comparator<ComputerMesureData>() {
                @Override
                public int compare(ComputerMesureData o1, ComputerMesureData o2) {
                    if (o1.getAvgAR()<o2.getAvgAR()){
                        return -1;
                    }else if (o1.getAvgAR()>o2.getAvgAR()){
                        return 1;
                    }else {
                        return 0;
                    }

                }
            });

            ComputerMesureData maxMQ1= Collections.max(datas, new Comparator<ComputerMesureData>() {
                @Override
                public int compare(ComputerMesureData o1, ComputerMesureData o2) {
                    if (o1.getAvgMQ() < o2.getAvgMQ()){
                        return -1;
                    }else if (o1.getAvgMQ() > o2.getAvgMQ()){
                        return 1;
                    }else {
                        return 0;
                    }

                }
            });

            maxWeight = maxWeight1.getAvgWeight();
            maxAR =  maxAR1.getAvgAR();
            maxMQ =  maxMQ1.getAvgMQ();

        }
        maxWeight = (float)(maxWeight*1.2);
        maxAR = (float) (maxAR*1.2);
        maxMQ = (float) (maxMQ*1.2);

    }


}
